import Vue from 'vue'
import Router from 'vue-router'

//3.导入组件
import Content from "../components/Content"
import Main from "../components/Main"
import UserList from "../views/user/List"
import UserProfile from "../views/user/Profile";
import Login from "../views/Login";
import Page from "../views/Page";
import Home from "../views/Home";
import Welcome from "../components/Welcome";
import User from "../components/user/User";
import Logo from "../views/Logo";
import Rights from "../components/power/Rights";
import Roles from "../components/power/Roles";
import GoodsList from "../components/goods/List";
import Add from "../components/goods/Add";
import Params from "../components/goods/Params";
import Cate from "../components/goods/Cate";
import Order from "../components/order/Order";
import Report from "../components/report/Report";
//1.安装路由
Vue.use(Router)

//2.配置导出路由
const router = new Router({
  routes: [
    // 重定向设置
    {path:'/',redirect:"/Login"},
    //路由配置
    {
      //用来测试ElementUI
      path: "/content",
      name: "content",
      component: Content

    },
    {
      //没用
      path:"/main",
      name: "main",
      component: Main
    },
    {
      path:"/Login",
      name: "Login",
      component: Login,
      //路由嵌套：
      children:[
        {path:"/user/list",component:UserList},
        {path:"/user/profile",component:UserProfile}
      ]
    },
    {
      //过程就是  先访问的/home 把边框都渲染出来了 之后又重定向到了/welcome
      path: "/home",
      name: "Home",
      component: Home,
      redirect: 'welcome',
      children:[
        {path : '/welcome', component: Welcome},
        {path : '/users' , component: User},
        {path : '/rights' , component: Rights},
        {path : '/roles' , component: Roles},
        {path : '/goods',component:GoodsList},
        {path : '/goods/add',component:Add},
        {path :'/params',component:Params},
        {path: '/categories', component: Cate },
        {path: '/orders', component: Order},
        {path: '/reports',component: Report},
      ]
    },
    {
      //日历页面
      path:"/Page",
      name: "Page",
      component: Page
    },{
      path:"/logo",
      name: "Logo",
      component:Logo
    }
  ]
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 将要访问的路径
  //from 代表从哪一个路径跳转而来
  //next 是一个函数,表示放行
  //  next() 放行   next("/login")  强制跳转

  if(to.path === '/Login') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/Login')   //没登陆 就去登录
  next()  //登录了就放行
})

export default router
